<template>
	<view class="confirmPickup">
		<view class="top">
			<view v-if="address.id > 0" class="address" @click="jump('address', optionsParam)">
				<image :src="obtainImgUrl('icon/icon33.png')" mode="aspectFill"></image>
				<view class="right">
					<view class="txt">
						<text>收货人：{{address.realname}}</text>
						<text>{{address.mobile}}</text>
					</view>
					<view class="txt1">
						收货地址：{{address.address_name}}{{address.address_details}}
					</view>
					<view class="jt"></view>
				</view>
			</view>
			<view v-else class="noaddress" @click="jump('addOreditAddress', 'id=0&formType=1&' +optionsParam)">
				<image :src="obtainImgUrl('icon/noaddress.png')" mode="widthFix"></image>
				<text>+新建地址</text>
			</view>
			<image class="img" :src="obtainImgUrl('icon/icon34.png')" mode="aspectFill"></image>
		</view>
		<view class="main">
			<view class="advView" v-if="info.cover != ''">
				<image class="img" :src="info.cover"  mode="widthFix"></image>
			</view>
			<view class="top">
				<view class="title">{{info.title}}</view>
				<view class="numView">
					<view>总条数：{{info.totalNum}}</view>
					<view @click="showUseList()">已提货：{{info.useNum}}</view>
				</view>
				<view>卡号：{{info.code}}</view>
				<view class="numView">
					<view>有效期：{{info.endTime}}</view>
				</view>
			</view>
			<view class="boxList">
				<view :class="'goods' + (isCheckItem(index) ? ' nowSel' : '') + (item.useState == 1 ? ' ok' : ' no')" v-for="(item,index) in productList" :key="index" @click="subCheckItem(index)">
					<image :src="item.picture" mode="aspectFill"></image>
					<view class="right">
						<text class="title">{{item.name}}</text>
						<text>{{item.sku_name}} {{item.num}}{{item.unit}}</text>
					</view>
					<view v-if="item.useState != 1" :class="'stateName state' + item.useState">{{item.useStateName}}</view>
					<view v-if="item.useState == 1" :class="'selAdd ' + (isCheckItem(index) ? ' nowSel' : '')">{{isCheckItem(index) ? '√' : '+'}}</view>
					<!-- <uni-icons v-if="isCheckItem(index)" class="selIcon" type="checkmarkempty" size="18" color="#fff" /> -->
 				</view>
			</view>
		</view>
		<view class="bottom" style="z-index: 10;">
			<view class="btn" @click="createOrder()">确定提货</view>
		</view>
		<uni-popup ref="popup" type="center">
			<view class="popupInfo">
				<view class="pickupTitle">提货记录</view>
				<image @click="close" class="close" :src="obtainImgUrl('icon/close.png')" mode="aspectFill"></image>
				<view class="popupItem" v-for="(item, index) in info.useList" :key="index">
					<view class="pickupSn">提货编号：{{item.order_sn}}</view>
					<view class="pickupDate">提货时间：{{item.created_time}}</view>
					<view class="pickupNum">提货数量：{{item.num}}</view>
					<view class="pickupBtn">
						<view class="btn" @click="jump('order_view', item.order_id)">详情</view>
					</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import serve from '../../static/request.js';
	import common from '../../static/jump.js';
	export default {
		data() {
			return {
				id: 0,
				info: {
					cover: '',
					title: '',
					code: '',
					totalNum: 0,
					useNum: 0,
					endTime: '',
					nextDate: '',
					useList: [],
				},
				productList: [],
				selList: [],
				address: {},
				formData: {
					card_id: 0,
					address_id: 0,
					data: "",
				},
				optionsParam: '',
				check_sub: false,
			}
		},
		methods: {
			resetAddress(str) {
				this.formData.address_id = str.id;
				this.address = str;
			},
			numFilter(value) {
				let tempVal = parseFloat(value).toFixed(3);
				let realVal = tempVal.substring(0, tempVal.length - 1);
				return realVal;
			},
			isCheckItem(k) {
				return this.selList.includes(k);
			},
			subCheckItem(k) {
				let item = this.productList[k];
				if(item.useState != 1) {
					return ;
				}
				const index = this.selList.indexOf(k);
				if(index !== -1) {
					this.selList.splice(index, 1);
				} else {
					this.selList.push(k);
				}
			},
			previewPickup() {
				serve.request({
					url: '/store/order/pickup',
					data: {
						id: this.id
					}
				}).then(res => {
					if (res.code != 200) {
						uni.showModal({
							title: '提示',
							content: res.message,
							showCancel: false,
							success: function() {
								common.jump('return_one');
							}
						});
						return;
					}
					this.address = res.data.address;
					this.productList = res.data.productList;
					this.info = res.data.info;
					this.formData.card_id = this.info.id;
					this.formData.address_id = this.address.id;
					this.$forceUpdate();
				});
			},
			showUseList() {
				if(this.info.useList.length <= 0) {
					return ;
				}
				this.$refs.popup.open();
			},
			close() {
				this.$refs.popup.close();
			},
			createOrder() {
				if (this.formData.address_id == 0) {
					uni.showToast({
						title: "请选择收货地址",
						icon: 'none'
					})
					return;
				}
				if (this.selList.length <= 0) {
					uni.showToast({
						title: "请点击选择提货商品",
						icon: 'none'
					})
					return;
				}
				this.formData.data = [];
				for(let i = 0; i < this.selList.length; i++) {
					let k = this.selList[i];
					this.formData.data.push(this.productList[k].dataId);
				}
				this.formData.data = JSON.stringify(this.formData.data);
				let _self = this;
				if (_self.check_sub == true) {
					return;
				}
				uni.showLoading({
					title: '提交中···',
					mask: true
				});
				_self.check_sub = true;
				serve.request({
					url: '/store/order/sub-pickup',
					data: _self.formData
				}).then(res => {
					uni.hideLoading();
					_self.check_sub = false;
					if(res.code != 200) {
						uni.showModal({
							title: '提示',
							content: res.message,
							showCancel: false,
						});
						return;
					} 
					common.jump('success', 'type='+res.data.type+'&id='+res.data.id);
					return ;
				});
			},
			obtainImgUrl(imgUrl) {
				return serve.imgUrl()+imgUrl;
			},
			jump(jump_type, jump_link) {
				common.jump(jump_type, jump_link);
			},
		},
		onLoad(options) {
			if (uni.getStorageSync('access-token') == '') {
				common.jump('login');
				return;
			}
			this.id = options.id;
			this.previewPickup();
		},
		onPullDownRefresh() {
			console.log('refresh');
			this.previewPickup();
		}
	}
</script>

<style scoped lang="scss">
	@media only screen and (device-width: 375px) and (device-height: 812px) and (-webkit-device-pixel-ratio: 3) {
		.confirmPickup {
			padding-bottom: 124upx !important;

			.bottom {
				padding-bottom: 12px !important;
			}
		}
	}

	.confirmPickup {
		padding-bottom: 100upx;
		
		.red {
			color: red;
		}

		.bottom {
			z-index: 100;
			position: fixed;
			width: 100%;
			bottom: 0upx;
			border-top: 1upx rgba(235, 236, 240, 1) solid;
			align-items: center;
			display: flex;
			box-sizing: border-box;
			height: 120upx;
			background: rgba(255, 255, 255, 1);
			box-shadow: 0px 1px 0px 0upx rgba(235, 236, 240, 1);
			padding-left: 30upx;
			font-size: 24upx;
			color: rgba(60, 72, 104, 0.5);
			padding-bottom: env(safe-area-inset-bottom);    
			padding-top: 20rpx;

			.btn {
				position: absolute;
				height: 100%;
				width: 100%;
				background: rgba(235, 89, 56, 1);
				line-height: 100upx;
				text-align: center;
				font-size: 28upx;
				font-weight: bold;
				color: rgba(255, 255, 255, 1);
				top: 0;
				right: 0;
			}
		}

		.top {
			position: relative;
			border-bottom: 16upx rgba(251, 251, 253, 1) solid;

			.noaddress {
				height: 190upx;
				width: 100%;
				display: flex;
				justify-content: center;
				align-items: center;

				image {
					width: 40upx;
				}

				text {
					font-size: 32upx;
					font-weight: bold;
					color: rgba(235, 89, 56, 1);
					margin-left: 10upx;
				}
			}

			.address {
				display: flex;
				position: relative;
				padding: 53upx 70upx;
				font-size: 28upx;
				font-weight: bold;
				align-items: center;

				image {
					position: absolute;
					left: 30upx;
					width: 31upx;
					height: 37upx;
					top: 120upx;
				}

				.txt {
					display: flex;
					justify-content: space-between;
					padding-bottom: 28upx;
				}

				.jt {
					position: absolute;
					right: 30upx;
					top: 105upx;
					content: '';
					border: 1upx solid rgba(57, 72, 107, 0.5);
					border-width: 0 3upx 3upx 0;
					padding: 6upx;
					transform: rotate(-45deg);
					-webkit-transform: rotate(-45deg);
				}
			}

			.img {
				position: absolute;
				bottom: 0;
				height: 10upx;
				width: 100%;
			}
		}

		.main {
			width: 100%;

			.top {
				padding: 10rpx 30rpx 30rpx 30rpx;
				border-top: 1upx rgba(235, 236, 240, 1) solid;
				border-bottom: 1upx rgba(235, 236, 240, 1) solid;
				.title {
					font-size: 42rpx;
				}
				.numView {
					display: flex;
					justify-content: space-between;
					align-items: center;
					margin: 10rpx 0;
				}
			}
			.boxList {
				padding: 20rpx;
				margin-bottom: 50rpx;
			}
			.goods {
				width: 95vw;
				border: 2rpx solid #ccc;
				padding: 10rpx;
				border-radius: 10rpx;
				box-sizing: border-box;
				display: flex;
				align-items: center;
				margin-bottom: 10rpx;
				position: relative;
				
				image {
					width: 100upx;
					height: 100upx;
					margin-right: 10upx;
				}
				.right {
					position: relative;
					display: flex;
					justify-content: center;
					flex-direction: column;
					font-size: 24rpx;
					
					.title {
						font-size: 32rpx;
						margin-bottom: 10rpx;
					}
				}
				.selIcon {
					position: absolute;
					right: 0;
					bottom: 0;
					background: #f00;
				}
				.selAdd {
					position: absolute;
					right: 10rpx;
					bottom: 20rpx;
					border-radius: 50%;
					width: 36rpx;
					height: 36rpx;
					font-size: 36rpx;
					color: #fff;
					text-align: 36rpx;
					display: flex;
					justify-content: center;
					align-items: center;
					border: 1rpx solid #ff00ff;
					background: #ff00ff;
				}
				.selAdd.nowSel{
					border: 1rpx solid #ff0000;
					background: #ff0000;
				}
			}
			.ok {
				
			}
			.no {
				background-color: #f0f0f0;
			}
			.nowSel {
				border: 2rpx solid #f00;
			}
			.stateName {
				position: absolute;
				top: 0;
				right: 8rpx;
				font-size: 28rpx;
			}
			.state1 {
				color: #ff00ff;
			}
			.state2 {
				color: #ffd600;
			}
			.state3 {
				color: #ff0000;
			}
		}
		
		.checkbox {
			width: 40upx;
			height: 40upx;
			line-height: 60upx;
			text-align: center;
			margin: 0 20upx 0 10upx;
			border: 2upx solid #888;
			color: #999;
			font-size: 28upx;  
		}
		.checked {
			border: none;
			background: url(http://app.shanhaiyouyu.com/attachment/xcx/my/checkbox_check.png);
			background-size: 100% auto;
		}

		.advView{
			.img{
				width: 100vw;
				border-radius: 6upx;
			}
		}
		.popupInfo {
			position: relative;
			z-index: 200;
			width: 90vw;
			background: #FFFFFF;
			padding: 20rpx;
			.pickupTitle {
				text-align: center;
				padding: 20rpx 0;
				border-bottom: 2rpx solid #ccc;
				margin-bottom: 20rpx;
			}
			.close {
				position: absolute;
				width: 40upx;
				height: 40upx;
				right: 10upx;
				top: 10upx;
			}
			.popupItem {
				padding: 20rpx;
				margin-bottom: 20rpx;
				border: 1rpx solid #ccc;
				border-radius: 20rpx;
				display: flex;
				flex-direction: row;
				flex-wrap: wrap;
				justify-content: flex-start;
				.pickupSn {
					width: 100%;
					margin-bottom: 10rpx;
				}
				.pickupDate {
					width: 100%;
					margin-bottom: 10rpx;
				}
				.pickupNum {
					width: 80%;
				}
				.pickupBtn {
					width: 120rpx;
					.btn {
						width: 120rpx;
						text-align: center;
						border: 1rpx solid #f60;
						background: #f60;
						color: #fff;
						border-radius: 10rpx;
					}
				}
			}
		}
	}
</style>